package com.example.seakdreamandroid.firstPage;

import android.content.Context;
import android.graphics.Outline;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

public class MyBannerAdapter extends BannerAdapter {
    private static final String TAG = "Adapter";
    private Context context;
    public MyBannerAdapter(List<String> datas,Context context) {
        super(datas);
        this.context = context;
    }

    @Override
    public Object onCreateHolder(ViewGroup parent, int viewType) {
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        params.leftMargin=20;
        params.rightMargin=20;
        linearLayout.setLayoutParams(params);
        linearLayout.setClipToOutline(true);
        linearLayout.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),40);
            }
        });
        return new ViewHolder(linearLayout);
    }

    @Override
    public void onBindView(Object holder, Object data, int position, int size) {
        ViewHolder viewHolder = (ViewHolder) holder;
        String imageUrl = (String)data;
        System.out.println(imageUrl);
        Glide.with(context).load(imageUrl).into(viewHolder.imageView);
        Log.i(TAG,"onBindView(),theBind：(" + viewHolder.index + "," + data + "," + position + "," + size + ")");
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        static int count = 0;
        public int index = 0;
        private static final String TAG = "ViewHolder";
        private LinearLayout linearLayout;
        private ImageView imageView;

        public ViewHolder(@NonNull LinearLayout linearLayout) {
            super(linearLayout);
            Log.i(TAG,"ViewHolder(LinearLayout linearLayout)，theCreate：" + ++count);
            this.linearLayout = linearLayout;
            ImageView imageView = new ImageView(linearLayout.getContext());
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            linearLayout.addView(imageView);
            this.imageView = imageView;
            index = count;
        }
    }


}
